import Vue from 'vue'
import Vuex from 'vuex'
import $config from './config.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		islogin: false,
		member: {},
		token: '',
		token_expired: 0,
		nickName: {},
		totals: 0,

		__key_member: 'usemall_member',
		__key_token: 'uni_id_token',
		__key_token_expired: 'uni_id_token_expired',
		__key_nickName: 'uni_id_nickName',
		__key_totals: 'uni_id_totals',
		__key_isLogin: 'islogin',
	},
	mutations: {
		// 登录成功
		login(state, res) {
			console.log(res, "<====== res1");
			// 用户已登录
			state.islogin = true;
			
			state.member = res;
			console.log(state.member,"<==============storemember")
			console.log(state,"<========state");
			state.token = res?.token;
			state.token_expired = res?.tokenExpired;
			state.nickName =res?.nickName;
			state.totals =res?.totals;

			// 存储会员 member 数据
			// uni.setStorage({
			// 	key: state.__key_member,
			// 	data: state.member
			// })
			uni.setStorageSync(state.__key_member, state.member);
			// 存储uid token 数据
			// uni.setStorage({
			// 	key: state.__key_token,
			// 	data: state.token
			// })
			
			// // 存储uid nickName 数据
			// uni.setStorage({
			// 	key: state.__key_nickName,
			// 	data: state.nickName
			// })
			
			// 存储 islogin 数据
			// uni.setStorage({
			// 	key: state.__key_isLogin,
			// 	data: state.islogin
			// })
			
			// 存储uid token_expired 数据
			// uni.setStorageSync({
			// 	key: state.__key_token_expired,
			// 	data: state.token_expired
			// })
			uni.setStorageSync(state.__key_token_expired, state.token_expired);
		},
		
		// 加载 Token
		loadToken(state) {
			state.member = uni.getStorageSync(state.__key_member);
			state.token = uni.getStorageSync(state.__key_token);
			state.token_expired = uni.getStorageSync(state.__key_token_expired);
			// state.nickName = uni.getStorageSync(state.__key_nickName);
			// state.islogin = uni.getStorageSync(state.__key_isLogin);
			console.log(state.token_expired,"state.token_expired");
			if (state.token_expired > new Date().getTime()) {
				state.islogin = true;
			}
		},

		// 注销 
		logout(state) {
			state.islogin = false;
			state.member = {};
			state.token = '';
			state.token_expired = 0;
			// state.nickName = {};

			uni.removeStorage({
				key: state.__key_member
			})
			uni.removeStorage({
				key: state.__key_token
			})
			uni.removeStorage({
				key: state.__key_token_expired
			})
			
			uni.removeStorage({
				key: state.__key_isLogin
			})
			// uni.removeStorage({
			// 	key: state.__key_nickName
			// })
		},
		
		// token 令牌
		token(state, token, token_expired) {
			state.token = token;
			state.token_expired = token_expired;

			// 存储uid token 数据
			uni.setStorage({
				key: state.__key_token,
				data: state.token
			})
			// 存储uid token_expired 数据
			uni.setStorage({
				key: state.__key_token_expired,
				data: state.token_expired
			})
		},
		
		// 修改 member 数据
		putMember (state, user) {
			state.member = user;
			
			// 存储会员 member 数据
			uni.setStorage({
				key: state.__key_member,
				data: state.member
			})
		},
		
	// 	// 修改 nickName 数据
	// 	putNickName (state, nickName) {
	// 		state.nickName = nickName;
			
	// 		// 存储会员 nickName 数据
	// 		uni.setStorage({
	// 			key: state.__key_nickName,
	// 			data: state.nickName
	// 		})
	// 	}
	},
	actions: {

	}
})

export default store
